const base64 = require('../../utils/uploadImg/base64.js');
import {
  get,
  post
} from '../../request/index'
let timer;
const app = getApp()
let API_BASE_URL = app.globalData.baseURL.baseURL
Page({

  /**
   * 页面的初始数据
   */
  data: {
    code: "",
    codeMore: "",
    prolist: wx.getStorageSync('prolist') == '' ? wx.getStorageSync('prolist') : JSON.parse(wx.getStorageSync('prolist')),
    show: false,
    showBOBlist: [],
    metShow: true

  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(query) {

    this.getlogin()


  },

  site() {
    let pointlist = wx.getStorageSync('prolist') == '' ? wx.getStorageSync('prolist') : JSON.parse(wx.getStorageSync('prolist'));

    post('/app/siteMeeting/getTodayList', {
      mineId: pointlist.mineId
    }).then(res => {

      this.setData({
        show: true,
        showBOBlist: res.data.list
      })
    })
  },
  onClose() {
    this.setData({
      show: false
    })
  },
  taskpop(e) {

    let Id = e.currentTarget.dataset.item.id
    let pointlist = wx.getStorageSync('prolist') == '' ? wx.getStorageSync('prolist') : JSON.parse(wx.getStorageSync('prolist'));

    get(`/app/siteMeeting/scan/${Id}/${pointlist.userid}}`).then(res => {
      console.log(res);
      if (res.msg == "success") {
        wx.showToast({
          title: '签到成功',
          duration: 3000,
          icon: 'none',
          mask: true
        })
      } else {
        wx.showToast({
          title: res.msg,
          duration: 3000,
          icon: 'none',
          mask: true
        })
      }
    })
  },

  getlogin() {
    let that = this
    wx.login({
      success(res) {
        if (res.code) {

          that.setData({
            code: res.code
          })

        } else {
          console.log('登录失败！' + res.errMsg)
        }
      }
    })
  },
  btn_stop() {
    wx.navigateTo({
      url: '/pages/meeting/meeting'
    })
  },

  getinfo(openid) {
    let that = this
    wx.request({
      url: `${API_BASE_URL}/uaa/oauth/token?username=${openid}&password=8&grant_type=password&scope=app`, //仅为示例，并非真实的接口地址
      method: "POST",
      header: {
        'content-type': 'application/json',
        'Authorization': "Basic " + (base64.encode("app:123456")) // 默认值
      },
      success(res) {
        let {
          data
        } = res

        wx.setStorageSync('token', JSON.stringify(data.access_token));






      }
    })

  },

  getPhoneNumber(e) {

    let that = this;
    const {
      errMsg,
      iv,
      encryptedData
    } = e.detail


    if (errMsg == "getPhoneNumber:fail user deny") {
      return
    }

    wx.checkSession({
      success(res) {

        if (that.data.code) {

          get('/wx/wx/getUserInfoByCode', {
            code: that.data.code,
            encryptedData: encryptedData,
            iv: iv,
          }).then(res => {
            console.log(res);
            if (res.data == null) {
              that.getlogin()
              wx.showToast({
                title: '请到平台激活再使用',
                duration: 5000,
                icon: 'none',
                mask: true
              })
            } else {
              if (res.data.userid === "") {
                wx.showToast({
                  title: '您还未注册请先注册',
                  duration: 3000,
                  mask: true
                })
                wx.navigateTo({
                  url: `/pages/register/register?openid=${res.data.openid}`
                })
                return
              }


              if (!res.data.status) {
                wx.showToast({
                  title: '请重新登录',
                  duration: 3000,
                  mask: true
                })

                return
              } else {
                wx.setStorageSync('prolist', JSON.stringify(res.data))
                that.getinfo(res.data.openid);
                that.setData({
                  show: false,
                  loading: false,
                  show1: false,
                  show2: false
                });

                if (res.data.roleId.includes(",")) {
                  let arr = res.data.roleId.split(",")
                  // let arr = ['236','6']
                  if(arr.includes('236') || arr.includes('239')){
                    that.setData({
                      codeMore: "1",
                      metShow: false
                    }) 
                  }else{
                    that.setData({
                      codeMore: "2",
                      metShow: false
                    })
                  }
                
                
                } else {

                  if (res.data.roleId == "236" || res.data.roleId === '239') {
                    that.setData({
                      codeMore: "1",
                      metShow: false
                    })
                  } else {
                    that.setData({
                      codeMore: "2",
                      metShow: false
                    })
                  }
                }

              }



            }



          })
        } else {
          console.log('登录失败！' + res.errMsg)
        }
      },

    })

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})